﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using IAC_2013.com.iac.util;
using IAC_2013.com.iac.form;

namespace IAC_2013.com.iac.action
{
    class ProductCategoryAction
    {
        SqlConnection conn = new SqlConnection(DBUtils.CONNECTION_STRING);
        SqlCommand cmd;
        DataTable tb;
        SqlDataAdapter da;
        public Boolean addCategory(ProductCategoryForm frm)
        {
            string sql = "insert into sp_loai_san_pham values(";
            sql += "N'"+ frm.productCategoryName +"', ";
            sql += "N'"+ frm.description +"')";

            try 
            {
                cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                cmd.Dispose();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public Boolean editCategory(ProductCategoryForm frm)
        {
            string sql = "update sp_loai_san_pham set ";
            sql += " ten_loai_sp = N'"+ frm.productCategoryName +"', ";
            sql += " mo_ta = N'"+ frm.description +"' ";
            sql += " where ";
            sql += "id_lsp = " + frm.productCategoryId;

            try
            {
                cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                conn.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public Boolean deleteCategory(ProductCategoryForm frm)
        {
            string sql = "delete from sp_loai_sp where id_lsp = " + frm.productCategoryId;

            try
            {
                cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                conn.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public DataTable searchCategory(ProductCategoryForm frm)
        {
            tb = new DataTable();
            string sql = "select * from sp_loai_san_pham lsp where 1 = 1";
            if (frm.productCategoryName != null && frm.productCategoryName != "")
            {
                sql += " and LOWER(lsp.ten_loai_sp) LIKE '" + "%" + frm.productCategoryName + "%" +"'";
            }
            try
            {
                cmd = new SqlCommand(sql, conn);
                conn.Open();
                da = new SqlDataAdapter(cmd);
                tb = new DataTable();
                da.Fill(tb);
                return tb;
            }
            catch (Exception)
            {
                return tb;
            }
        }
    }
}
